package com.suse.warmstill.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suse.warmstill.common.model.CallResult;
import com.suse.warmstill.common.model.ListPageModel;
import com.suse.warmstill.entity.TbMenu;
import com.suse.warmstill.service.BaseService;
import com.suse.warmstill.service.MenuService;
import com.suse.warmstill.vo.ConditionVO;
import com.suse.warmstill.vo.MenuOption;
import com.suse.warmstill.vo.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author 黄靖
 * @author 2022-10-20 00:42
 * description:
 **/
@CrossOrigin
@Slf4j
@Api("角色菜单的操作，部门老大可以操作")
@RequestMapping("/warmstill/menus")
@RestController
public class PCMenuApi {
    @Autowired
    private MenuService menuService;

    @Autowired
    private BaseService<MenuVo, TbMenu> baseService;


    @ApiOperation("查询所有的 详细Menu，前端不显示")
    @GetMapping("/listMenus")
    public CallResult<ListPageModel<MenuVo>> listMenus(@Validated @RequestParam(required = true) ConditionVO conditionVO) {

        final Page<TbMenu> tbMenuPage = menuService.listMenus(conditionVO);
        final ListPageModel<MenuVo> listPageModel = new ListPageModel<>();


        final List<MenuVo> menuVos = new ArrayList<>();


        //page
        final List<TbMenu> records = tbMenuPage.getRecords();

        for (TbMenu entity : records
        ) {
            final MenuVo menuVo = baseService.toVo(entity);
            /*
             * 查询 子菜单
             * */
            Set<MenuVo> childerSet = menuService.getChildren(entity.getId());
            if (ObjectUtils.isNotEmpty(childerSet)) {
                //   menuVo.setChildren(childerSet);
            }

            menuVos.add(menuVo);
        }

        listPageModel.setList(menuVos);
        listPageModel.setPageSize((int) tbMenuPage.getSize());
        listPageModel.setPageCount((int) tbMenuPage.getPages());
        listPageModel.setDataSize((int) tbMenuPage.getTotal());
        listPageModel.setCurrentPage((int) tbMenuPage.getCurrent());


        return CallResult.success(listPageModel);
    }


    // @RoleCheck(path = "/warmstill/menus/listMenuOption",limitedRoleCodeList = "333")
    @ApiOperation("查询所有的 menu 前端可以显示")
    @GetMapping("/listMenuOption")
    public CallResult<List<MenuOption>> listMenuOption(@Validated ConditionVO conditionVO) {
        List<MenuOption> listMenuOptions = menuService.listMenuOption(conditionVO);
        return CallResult.success(listMenuOptions);
    }


    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @ApiOperation(("插入一条"))
    @PostMapping("/inserOne")
    public CallResult<Void> insertOne(@Validated @RequestBody MenuVo menuVo) {
        menuService.inserOne(menuVo);
        return CallResult.success();
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @PostMapping("/updateMenu")
    @ApiOperation("修改 Menu")
    public CallResult<Void> updateMenu(@Validated @RequestBody MenuVo menuVo) {
        menuService.update(menuVo);
        return CallResult.success();
    }


    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @DeleteMapping("/Logicdelete/{id}")
    @ApiOperation("逻辑删除")
    public CallResult<Void> logicDeleteOne(@PathVariable("id") Long id) {


        menuService.logicDeleteOne(id);
        return CallResult.success();
    }


}
